require 'rake'

desc "Functions for manipulating blog users"
namespace :user do

  desc "Add a new user to the blog"
  task(:add => :environment) do
    user = User.new()
    puts "login:"
    user.login = STDIN.readline.strip
    puts "password:"
    user.password = STDIN.readline.strip
    puts "email:"
    user.email = STDIN.readline.strip
    puts "admin (yes/no):"
    admin = STDIN.readline.strip
    if admin == "yes" or admin == "y"
      user.is_administrator = true
    end
    if user.save
      puts "User created"
    else
      puts "User not saved"
    end
  end
  
  desc "List users in the blog"
  task(:list => :environment) do
    users = User.find(:all)
    puts "Users in DB:"
    puts "Login, Email, Admin, Last Login"
    for user in users
      puts "#{user.login}, #{user.email}, #{"admin" if user.is_administrator}, #{user.last_login_at}"
    end
  end

  desc "Delete a user from the blog"
  task(:delete => :environment) do
    puts "login to delete:"
    login = STDIN.readline.strip
    user = User.find(:first, :conditions => ["login = ?", login])
    user.destroy
    puts "deleted"
  end

end